﻿using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using AvalonDock;
using Dispatcher.ViewModels;
using Repository;

namespace Dispatcher.Views
{
    /// <summary>
    /// Interaction logic for PlaceSearchView.xaml
    /// </summary>
    public partial class PlaceSearchView
    {
        /// <summary>
        /// View of places.
        /// </summary>
        private readonly ContentListView _view = new ContentListView();

        /// <summary>
        /// Initializes a new instance of the <see cref="PlaceSearchView"/> class.
        /// </summary>
        public PlaceSearchView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Checks the specified check box.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="checkBox">The check box.</param>
        /// <param name="list">The list.</param>
        public static void Check<TEntity>(ToggleButton checkBox, ICollection<TEntity> list)
        {
            var item = checkBox.DataContext;
            if (checkBox.IsChecked == true && !list.Contains((TEntity)item))
            {
                list.Add((TEntity)item);
            }
            else
            {
                list.Remove((TEntity)item);
            }
        }

        /// <summary>
        /// Handles the Checked event of the CheckBox control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void CheckBox_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
            var viewModel = (PlaceSearchViewModel)DataContext;
            var checkBox = (CheckBox)sender;

            if (checkBox.DataContext is Location)
            {
                Check(checkBox, viewModel.RequiredLocations);
            }

            if (checkBox.DataContext is PlaceType)
            {
                Check(checkBox, viewModel.RequiredPlaceTypes);
            }

            if (checkBox.DataContext is EquipmentType)
            {
                Check(checkBox, viewModel.RequiredEquipmentTypes);
            }

            if (checkBox.DataContext is Equipment)
            {
                Check(checkBox, viewModel.RequiredEquipments);
            }

            if (checkBox.DataContext is Place)
            {
                Check(checkBox, viewModel.SelectedPlaces);
            }

            if (checkBox.DataContext is Event)
            {
                Check(checkBox, viewModel.SelectedReservedEvents);
            }
        }

        /// <summary>
        /// Handles the Click event of the Button control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            var viewModel = (PlaceSearchViewModel)DataContext;
            _view.CommonListView.ItemsDataGrid.CanUserAddRows = false;
            _view.CommonListView.ItemsDataGrid.IsReadOnly = true;
            _view.CommonListView.ItemsDataGrid.ItemsSource = viewModel.FreePlaces;
            _view.Show(MainWindow.DockingManager, AnchorStyle.Bottom);
        }
    }
}
